package com.mindainfo.groundwave.server.base.aop;

import com.alibaba.fastjson.JSON;
import com.mindainfo.groundwave.server.base.annotation.Log;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.SourceLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * Created by MD-01 on 2017/5/18.
 */
@Aspect
@Component
public class PermissionAspect {

    private Logger logger = LoggerFactory.getLogger("AOP拦截注解");

    @Pointcut("@annotation(com.mindainfo.groundwave.server.base.annotation.Log)")
    public void serverAspect(){

    }

    @Before("serverAspect()")
    public  void doBefore(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        logger.info("前置通知,方法:"+name);
    }

    @Around(value = "@annotation(log)")
    public Object aroundMethod2(ProceedingJoinPoint point,Log log){

        String name = point.getSignature().getName();
        String dsc = log.desc();
        System.out.println("name:"+name);
        System.out.println("dsc:"+dsc);
        Object[] args = point.getArgs();
        System.out.println("args:" + JSON.toJSONString(args));
        String kind = point.getKind();
        System.out.println("kind:"+kind);

        String canonicalName = point.getSignature().getDeclaringType().getCanonicalName();
        System.out.println("canonicalName:"+canonicalName);

        Method[] declaredMethods = point.getSignature().getDeclaringType().getDeclaredMethods();

        for(Method method : declaredMethods){
             System.out.println("method:"+method.getName());
            Type[] genericParameterTypes = method.getGenericParameterTypes();
            for(Type type : genericParameterTypes){
                System.out.println("type:" + type.getTypeName());
            }
        }
        Signature signature = point.getSignature();
        String declaringTypeName = signature.getDeclaringTypeName();
        System.out.println("declaringTypeName:"+declaringTypeName);
        int modifiers = signature.getModifiers();
        System.out.println("modifiers:"+modifiers);

        Object proceed = null;
        try {
            proceed = point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}
